
$.ajax({
    type: "post",  //数据提交方式（post/get）
    url: "http://localhost:8088/api/username",  //提交到的url
    dataType: "json",//返回的数据类型格式
    xhrFields: {
      withCredentials: true
     },
    defaults: {
      widthCredentials: true
    },
    crossDomain: true,
    success:   function (res) {
		// // status 为 0 表示获取用户名称成功；否则表示获取用户名称失败！
		// if (res.status !== 0) {
		//   alert('您尚未登录，请登录后再执行此操作！')
		// } else {
		//   alert('欢迎您：' + res.username)
		// }
	  }
  });

// 轮播图tap栏切换
const img =document.querySelector('.xtx_banner img')
let i=0;
const as=document.querySelectorAll('.xtx_category .xtx_category_super li')
for(let i=0;i<as.length;i++){
	as[i].addEventListener('mouseenter',function(){
	document.querySelector('.xtx_category .xtx_category_super .active').classList.remove('active')
	this.classList.add('active')
	
	document.querySelector('.xtx_banner .active').classList.remove('active')
	document.querySelector(`.xtx_banner .item:nth-child(${i + 1})`).classList.add('active')
	})
}
			

//电梯导航
			(function () {
			  const entry = document.querySelector('.xtx_entry')
			  const elevator = document.querySelector('.xtx-elevator')

			  window.addEventListener('scroll', function () {
			    const n = document.documentElement.scrollTop

			    elevator.style.opacity = n >= entry.offsetTop ? 1 : 0
			  })

			  const backTop = document.querySelector('#backTop')
			  backTop.addEventListener('click', function () {
			    window.scrollTo(0, 0)
			  })
			})();
			
			(function () {

			  const list = document.querySelector('.xtx-elevator-list')
			  list.addEventListener('click', function (e) {

			    if (e.target.tagName === 'A' && e.target.dataset.name) {

			      const old = document.querySelector('.xtx-elevator-list .active')

			      if (old) old.classList.remove('active')

			      e.target.classList.add('active')

			      const top = document.querySelector(`.xtx_goods_${e.target.dataset.name}`).offsetTop
			      document.documentElement.scrollTop = top
			
			    }
			  })
			
			  window.addEventListener('scroll', function () {
			    const old = document.querySelector('.xtx-elevator-list .active')

			    if (old) old.classList.remove('active')

			    const news = document.querySelector('.xtx_goods_new')
			    const popular = document.querySelector('.xtx_goods_popular')
			    const brand = document.querySelector('.xtx_goods_brand')
			    const topic = document.querySelector('.xtx_goods_topic')
			    const n = document.documentElement.scrollTop
			    if (n >= news.offsetTop && n < popular.offsetTop) {
			      // 选择第一个小盒子
			      document.querySelector('[data-name=new]').classList.add('active')
			    } else if (n >= popular.offsetTop && n < brand.offsetTop) {
			      document.querySelector('[data-name=popular]').classList.add('active')
			    } else if (n >= brand.offsetTop && n < topic.offsetTop) {
			      document.querySelector('[data-name=brand]').classList.add('active')
			    } else if (n >= topic.offsetTop) {
			      document.querySelector('[data-name=topic]').classList.add('active')
			    }
			  })
			
			})();
			  
			  
// 小爱心切换
    let flag=false
		const img1 = document.querySelector('.aiaixin1 img')
		const img2 = document.querySelector('.aiaixin2 img')
		const img3 = document.querySelector('.aiaixin3 img')
		img1.addEventListener('click',function(){
			if(flag){
				img1.src='./img/爱心.png'
				flag = !flag
			}
			else{
				img1.src='./img/爱心1.png'
				flag = !flag
			}
		})
		img2.addEventListener('click',function(){
			if(flag){
				img2.src='./img/爱心.png'
				flag = !flag
			}
			else{
				img2.src='./img/爱心1.png'
				flag = !flag
			}
		})
		img3.addEventListener('click',function(){
			if(flag){
				img3.src='./img/爱心.png'
				flag = !flag
			}
			else{
				img3.src='./img/爱心1.png'
				flag = !flag
			}
		})
			

//首页渲染登录的用户名和退出登陆
const li1 =document.querySelector('.xtx_navs li:first-child')
const li2 = li1.nextElementSibling

function render(){
  const uname = sessionStorage.getItem('xtx-uname')
	if(uname){
		li1.innerHTML = `
						<a href='#'>
							<img src="./img/iconfont/daibiao.png" style="width:13px";height:'13px'/>&nbsp;
								${uname}
						</a>`
		li2.innerHTML = `<a href='#'>退出登陆</i></a>`
	}else{
		li1.innerHTML = `<a href='./login.html'>请先登录</a>`
		li2.innerHTML = `<a href='./register.html'>免费注册</a>`
	}
}

render()

//退出登陆
li2.addEventListener('click',function(){
	sessionStorage.removeItem('xtx-uname')
	render()
})



// //视频播放
// const video = document.querySelector('video')
//     video.ontimeupdate = _.throttle(() => {
//       localStorage.setItem('currentTime', video.currentTime)
//     }, 1000)
//     // 打开页面触发事件，就从本地存储里面取出记录的时间， 赋值给  video.currentTime
//     video.onloadeddata = () => {
//       video.currentTime = localStorage.getItem('currentTime') || 0
//     }

    
